home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / ppp / dp-2.3 / dpd / paths.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-26  |  4.0 KB  |  231 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <memory.h>
  5. #include <ctype.h>
  6. #include <unistd.h>
  7.  
  8. #include "dpconf.h"
  9.  
  10. char conf_file[] = "/etc/dp.conf";
  11.  
  12. static var_val *dp_pathtab;
  13. static unsigned int n_dp_paths;
  14.  
  15. extern var_val dp_pathtab_init[];
  16.  
  17. /*
  18.  * Table lookup for path variables.
  19.  */
  20. var_val *
  21. find_var(var)
  22. char *var;
  23. {
  24.     var_val *vv;
  25.     if (!var)
  26.     return (var_val *)0;
  27.     for (vv = dp_pathtab ; vv->var ; vv++)
  28.     if (strcmp(vv->var, var) == 0)
  29.         return vv;
  30.     return (var_val *)0;
  31. }
  32.  
  33. var_val *
  34. new_var(var)
  35. char *var;
  36. {
  37.     var_val *vv = dp_pathtab + n_dp_paths;
  38.     dp_pathtab = (var_val *)realloc((char *)dp_pathtab,
  39.                     (++n_dp_paths + 1) * sizeof(*dp_pathtab));
  40.     (void)memset((char *)(dp_pathtab + n_dp_paths), 0, sizeof (*dp_pathtab));
  41.     vv->var = malloc((unsigned)strlen(var)+1);
  42.     (void)strcpy(vv->var, var);
  43.     return vv;
  44. }
  45.  
  46. /*
  47.  * Table lookup for path variables.
  48.  */
  49. char *
  50. map_var(var)
  51. char *var;
  52. {
  53.     var_val *vv;
  54.     return (vv = find_var(var)) ? vv->val : (char *)0;
  55. }
  56.  
  57.  
  58. init_pathconf()
  59. {
  60.     var_val *cv, *vv;
  61.     FILE *cf;
  62.     extern void yyerror();
  63.     
  64.     /*
  65.      * Start with defaults.
  66.      */
  67.     for (n_dp_paths = 0, vv = dp_pathtab_init ; vv->var ; vv++)
  68.     n_dp_paths++;
  69.     dp_pathtab = (var_val *)malloc((n_dp_paths + 1) * sizeof(*dp_pathtab));
  70.     (void)memcpy((char *)dp_pathtab,
  71.          (char *)dp_pathtab_init,
  72.          (int)(n_dp_paths + 1) * sizeof(*dp_pathtab));
  73.  
  74.     /*
  75.      * Read in config file, if present.
  76.      */
  77.     if (cf = fopen(conf_file, "r")) {
  78.     while (cv = readconf(conf_file, cf))
  79.         for ( ; cv->var ; cv++) {
  80.         if (!(vv = find_var(cv->var)))
  81.             vv = new_var(cv->var);
  82.         vv->val = malloc((unsigned)strlen(cv->val)+1);
  83.         (void)strcpy(vv->val, cv->val);
  84.         }
  85.     (void)fclose(cf);
  86.     }
  87.  
  88.     /*
  89.      * Resolve any variable paths..
  90.      */
  91.     for (vv = dp_pathtab ; vv->var ; vv++)
  92.     if (strchr(vv->val, '$'))
  93.         vv->val = expand_path(vv->val);
  94. }
  95.  
  96.  
  97. #define    istok(c)    ((isalnum(c)) || (c) == '_')
  98.  
  99. /*
  100.  * Returns the next character after the variable name.
  101.  * Deposits the value of the variable in val.
  102.  */
  103. char *
  104. expand_var(p0, val)
  105. char *p0, *val;
  106. {
  107.     char *p = p0,
  108.      ec,
  109.      *v,
  110.      var_name[128],
  111.      *vn = var_name;
  112.  
  113.     *val = '\0';
  114.  
  115.     switch (*++p) {
  116.      case '{':    ec = '}';  p++;    break;
  117.      case '(':    ec = ')';  p++;    break;
  118.      default:    ec = '\0';    break;
  119.     }
  120.     if (ec) {
  121.     while (*p && *p != ec)
  122.       *vn++ = *p++;
  123.     if (*p)
  124.         p++;
  125.     }
  126.     else
  127.     while (*p && istok(*p))
  128.       *vn++ = *p++;
  129.     *vn = '\0';
  130.     if ((!ec || *(p-1) == ec) && (v = map_var(var_name)))
  131.     (void)strcpy(val, v);
  132.     else {
  133.     (void)strncpy(val, p0, p-p0);
  134.     val[p-p0] = '\0';
  135.     }
  136.     return p;
  137. }
  138.  
  139. char *
  140. expand_path(op)
  141. char *op;
  142. {
  143.     char *np0, *np, *p;
  144.     unsigned l;
  145.     char varbuf[128];
  146.  
  147.     if (!dp_pathtab)
  148.     init_pathconf();
  149.  
  150.     for (p = op, l = 0 ; *p ;)
  151.     if (*p == '$') {
  152.         p = expand_var(p, varbuf);
  153.         l += strlen(varbuf);
  154.     }
  155.     else {
  156.         l++;
  157.         p++;
  158.     }
  159.  
  160.     np0 = malloc(l + 1);
  161.  
  162.     for (p = op, np = np0 ; *p ; ) {
  163.     if (*p == '$') {
  164.         p = expand_var(p, varbuf);
  165.         (void)strcpy(np, varbuf);
  166.         np += strlen(np);
  167.     }
  168.     else
  169.         *np++ = *p++;
  170.     }
  171.  
  172.     *np = '\0';
  173.  
  174.     return np0;
  175. }
  176.  
  177. char *
  178. expand_dir_file(dir, file)
  179. char *dir, *file;
  180. {
  181.     char *tmp,
  182.      *ep;
  183.     if (*file == '/')
  184.     return expand_path(file);
  185.     else {
  186.     tmp = malloc((unsigned)strlen(dir) + 1 + (unsigned)strlen(file) + 1);
  187.     (void)sprintf(tmp, "%s/%s", dir, file);
  188.     ep = expand_path(tmp);
  189.     (void)free(tmp);
  190.     return ep;
  191.     }
  192. }
  193.  
  194. char *
  195. expand_dirs_file(dirs, file)
  196. char *dirs, *file;
  197. {
  198.     char *dir;
  199.     char *d, *de, *p;
  200.  
  201.     for ( d = dirs ; ; d = de ) {
  202.     de = strchr(d, ':');
  203.     if (!de)
  204.         de =  d + strlen(d);
  205.  
  206.     dir = malloc((unsigned)(de - d + 1));
  207.     (void)strncpy(dir, d, de-d);
  208.     dir[de-d] = '\0';
  209.  
  210.     p = expand_dir_file(dir, file);
  211.  
  212.     /*
  213.      * If the file exists in this directory,
  214.      * or we are at the last path on our list,
  215.      * return this path.
  216.      */
  217.     if (!*de || (access(p, F_OK) == 0)) {
  218.         (void)free(dir);
  219.         return p;
  220.     }
  221.  
  222.     /*
  223.      * More to try..
  224.      */
  225.     (void)free(dir);
  226.     (void)free(p);
  227.     if (*de)
  228.         de++;
  229.     }
  230. }
  231.